package com.proton.carepatchtemp.utils;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;

import com.proton.carepatchtemp.application.VCare;

public class AppUtils {

    public static final String DOWN_LOAD_ADDRESS_TENCENT = "http://a.app.qq.com/o/simple.jsp?pkgname=com.proton.carepatchtemp";

    public static final boolean needUpdate(int versionCode) {
        int appVersionCode;
        PackageManager manager = VCare.get().getPackageManager();
        try {
            PackageInfo info = manager.getPackageInfo(VCare.get().getPackageName(), 0);
            appVersionCode = info.versionCode; //版本名
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return false;
        }
        return versionCode > appVersionCode;
    }

    public static String getVersionName() {
        String appVersionName;
        PackageManager manager = VCare.get().getPackageManager();
        try {
            PackageInfo info = manager.getPackageInfo(VCare.get().getPackageName(), 0);
            appVersionName = info.versionName; //版本名
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return "";
        }
        return appVersionName;
    }

    public static int getVersionCode() {
        int appVersionCode;
        PackageManager manager = VCare.get().getPackageManager();
        try {
            PackageInfo info = manager.getPackageInfo(VCare.get().getPackageName(), 0);
            appVersionCode = info.versionCode; //版本名
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return 0;
        }
        return appVersionCode;
    }


    public static int compareVersion(String version1, String version2) {

        if (TextUtils.isEmpty(version1) || TextUtils.isEmpty(version2)) {
            return -1;
        }

        if (version1.equalsIgnoreCase(version2)) {
            return 0;
        }

        version1 = version1.replaceAll("V", "");
        version2 = version2.replaceAll("V", "");

        String[] version1Array = version1.split("\\.");
        String[] version2Array = version2.split("\\.");

        int index = 0;
        int minLen = Math.min(version1Array.length, version2Array.length);
        int diff = 0;

        while (index < minLen && (diff = Integer.parseInt(version1Array[index]) - Integer.parseInt(version2Array[index])) == 0) {
            index++;
        }

        if (diff == 0) {
            for (int i = index; i < version1Array.length; i++) {
                if (Integer.parseInt(version1Array[i]) > 0) {
                    return 1;
                }
            }

            for (int i = index; i < version2Array.length; i++) {
                if (Integer.parseInt(version2Array[i]) > 0) {
                    return -1;
                }
            }

            return 0;
        } else {
            return diff > 0 ? 1 : -1;
        }
    }

}
